﻿using DICOM_PROJECT.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Text.RegularExpressions;

namespace DICOM_PROJECT.DAL
{
    public class HospitalContext:DbContext
    {
        public HospitalContext():base("HospitalContext")
        {
        }
        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<Patient> Patients { get; set; }
        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<Radiologist> Radiologists { get; set; }
        public DbSet<Visit> Visits { get; set; }
        public DbSet<Indication> Indications { get; set; }
        //public DbSet<ImageVisit> ImageVisits { get; set; }
        public DbSet<Role> Roles { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Visit>()
                    .HasRequired(m => m.CreateDoctor)
                    .WithMany(t => t.CreateVisits)
                    .HasForeignKey(m => m.CreateDoctorID)
                    .WillCascadeOnDelete(false);
            modelBuilder.Entity<Visit>()
                    .HasOptional(m => m.UpdateDoctor)
                    .WithMany(t => t.UpdateVisits)
                    .HasForeignKey(m => m.UpdateDoctorID)
                    .WillCascadeOnDelete(false);
            //modelBuilder.Entity<Indication>()
            //    .HasOptional(i => i.ImageVisit)
            //    .WithMany()
            //    .HasForeignKey(i => i.ImageVisitID);
            modelBuilder.Entity<UserProfile>()
                .HasMany(u => u.Roles)
                .WithMany(r => r.UserProfiles)
                .Map(m =>
                {
                    m.ToTable("UserRoles");
                    m.MapLeftKey("UserProfileID");
                    m.MapRightKey("RoleID");
                });
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            //HospitalContext.Database.ExecuteSqlCommand("ALTER TABLE Place ALTER COLUMN Id IDENTITY (1000,1)");
        }
    }
}